package com.example.springbootvuetest.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.springbootvuetest.mapper.IdleItemMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootvuetest.base.Result;
import lombok.RequiredArgsConstructor;
import com.example.springbootvuetest.entity.IdleItem;
import com.example.springbootvuetest.service.IIdleItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * <p>
 * controller
 * </p>
 *
 * @author shuoye
 * @since 2023-03-14
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "REST - ")
@RestController
@RequestMapping("/idle")
public class IdleItemController {
    private final IIdleItemService service;

    private final IdleItemMapper idleItemMapper;

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public Result createOne(@RequestBody IdleItem entity) {
        entity.setIdleStatus((byte) 1);
        boolean flag = service.save(entity);
        return flag ? Result.success(entity) : Result.error();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public Result delete(@PathVariable("id") Integer id) {
        boolean flag = service.removeById(id);
        return flag ? Result.success() : Result.error();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改物品状态")
    public Result update(@RequestBody IdleItem idleItem) {
        boolean flag = idleItemMapper.updateIdleStatus(idleItem.getId(), idleItem.getIdleStatus());
        return flag ? Result.success() : Result.error();
    }

    @PostMapping("/updateIdleBatch")
    @ApiOperation(value = "批量修改物品状态")
    public Result updateBatch(@RequestBody List<Integer> ids) {
        boolean flag = true;
        for (Integer id : ids) {
            if (!idleItemMapper.updateIdleStatus(id, (byte) 0))
                flag = false;
        }
        return flag ? Result.success() : Result.error();
    }

    @GetMapping("/info")
    public Result getIdleItem(@RequestParam Integer id) {
        return Result.success(service.getIdleItem(id));
    }

    @GetMapping("/find")
    @ApiOperation(value = "查询所有物品")
    public Result<Map<String, Object>> queryPage(@RequestParam(name = "findValue", defaultValue = "") String findValue,
                                                 @RequestParam(name = "pageNum") Integer pageNum,
                                                 @RequestParam(name = "pageSize") Integer pageSize,
                                                 @RequestParam(name = "labelName", defaultValue = "0") Integer labelName) {
        System.out.println(labelName);
        HashMap<String, Object> map = new HashMap<>();
        Map<String, Object> resMap = service.findIdleItem(findValue, pageNum, pageSize, 1, labelName);
        return Result.success(resMap);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询自己发布的所有物品")
    public Result queryAllOfMyItem(@RequestParam("userId") Integer userId) {
        QueryWrapper<IdleItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).orderByDesc("release_time");
        List<IdleItem> itemList = idleItemMapper.selectList(queryWrapper);
        return Result.success(itemList);
    }

}